/*************************************************************************
	> File Name: client.cpp
	> Author: xuwenlong
	> Mail: myxuan475@126.com 
	> Created Time: 2018年09月17日 星期一 15时02分07秒
 ************************************************************************/

#include "custom.h"
#include <event2/thread.h>
#include "customserver.h"
#include <map>
#include <dirent.h>
#include <utils/shellcmd.h>
#include "exchange.h"

Custom::Custom(CustomServer *pServer):
    User(pServer,new JsonRpcHandler(this))
{
}

Custom::~Custom()
{
}

BOOL Custom::OnReceiveJson(
        S32 index,
        DAS_RESULT status,
        const std::string &method,
        const Json::Value &result)
{
    S32 msgId = -1;
    AutoLock autolock(m_ireqLock);

    std::map<S32,ReqGuest>::iterator iter;
    for(iter = m_reqList.begin();iter != m_reqList.end();iter++) {
        ReqGuest req = iter->second;

        if (req.method == method ) {
            msgId = iter->first;

            m_reqList.erase(iter);
            m_pProtocolHandler->ReturnAsyncResult(
                        status,msgId,result);
            return TRUE;
        }
    }
    return TRUE;
}

DAS_RESULT Custom::requestAcquisitor(int waitId,const Json::Value &params, Json::Value &result)
{
    std::string strId = params["gatewayId"].asString();
    std::string second_method = params["method"].asString();
    Json::Value second_params = params["params"];

    AutoLock autolock(m_ireqLock);
    if (m_reqList.find(waitId) != m_reqList.end()) {
        return DAS_COLLISION;
    }
    ReqGuest req(strId,second_method);
    m_reqList.insert(std::pair<S32,ReqGuest>(waitId,req));

    if (-1 == ((CustomServer*)server())->Ex()->requestAcquisitor(this,strId,second_method,second_params))
        return DAS_DEVICENOTEXIST;

    return DAS_RESULT_MAX;
}

DAS_RESULT Custom::getAcquisitorList(const Json::Value &params, Json::Value &result)
{
    return ((CustomServer*)server())->Ex()->getAcquisitorList(params,result);
}

DAS_RESULT Custom::listLogs(const Json::Value &params, Json::Value &result)
{
    string strId = params["gatewayId"].asString();

    DIR *pdir;
    struct dirent *pdirent;
    char path[256];

    sprintf(path,"%s/log/%s",getenv(CONFIG_DIR_ENV),strId.c_str());
    pdir = opendir(path);

    if (pdir == NULL)
        return DAS_FILE_OPENFAILD;
    for (pdirent = readdir(pdir);pdirent != NULL;pdirent = readdir(pdir)) {
        if (strcmp(pdirent->d_name,".") == 0 ||
                strcmp(pdirent->d_name,"..") == 0)
            continue;
        if (pdirent->d_type != 8)
            continue;

        result.append(pdirent->d_name);
    }
    closedir(pdir);
    return DAS_SUCCESS;
}

DAS_RESULT Custom::filterLog(const Json::Value &params, Json::Value &result)
{
    string script = params.asString();

    ShellCmd shellCmd;
    string res = shellCmd.ShellGet(script.c_str());
    result = res;
    return DAS_SUCCESS;
}

DAS_RESULT Custom::getPort(const Json::Value &params, Json::Value &result)
{
    string strId = params["gatewayId"].asString();
    int type = params["type"].asInt();

    int port = 0;
    int time = 0;
    while (port == 0 && ++time <= 5) {
        port = ((CustomServer*)server())->Ex()->getPort(strId,type);
        if (port != 0)
            break;
        sleep(3);
    }

    result = port;
    return DAS_SUCCESS;

}

S32 Custom::SendJsonRpc(tJsonRpcInterface _interface,const std::string &method, const Json::Value &params)
{
    return m_pProtocolHandler->SendJsonRpc(_interface,method,params);
}
